package com.formiaomiao.demain.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.fragment.UpdatePasswordFragment;
import com.formiaomiao.demain.fragment.UpdateUserNameFragment;
import com.formiaomiao.demain.model.UserBean;
import com.formiaomiao.demain.util.MessageEvent;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.util.UseGlideUtils;
import com.formiaomiao.demain.weight.LoadingDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class UpdateUserActivity extends PicActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.tv_class_submit)
    TextView tvClassSubmit;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.rl_username)
    RelativeLayout rlUsername;
    @BindView(R.id.rl_password)
    RelativeLayout rlPassword;

    UpdatePasswordFragment passwordFragment;
    UpdateUserNameFragment usernameFragment;

    LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        ViewInit();
    }

    @Override
    public void formAlbum(String path) {
        LubanInit(path);
    }

    @Override
    public void formCamera(String path) {
        LubanInit(path);
    }

    @Override
    public void formCorp(String path) {
        LubanInit(path);
    }

    public void LubanInit(String path) {
        Luban.with(this)
                .load(path)
                .ignoreBy(100)
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {
                        // TODO 压缩开始前调用，可以在方法内启动 loading UI
                        loadingDialog.show();
                    }

                    @Override
                    public void onSuccess(File file) {
                        // TODO 压缩成功后调用，返回压缩后的图片文件
                        UpdataHead(file);
                    }

                    @Override
                    public void onError(Throwable e) {
                        // TODO 当压缩过程出现问题时调用
                        loadingDialog.dismiss();
                    }
                }).launch();
    }

    /**
     * 修改头像
     *
     * @param file
     */
    public void UpdataHead(final File file) {
        HttpParams params = new HttpParams();
        params.put("id",StaticValues.userid);
        params.put("headimg",file);
        OkGo.<String>post(BaseUrl.USERUPDATEHEAD).
                headers("Authorization", PreferenceUtils.getString(this,PreferenceValues.TOKEN)).
                params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if(response.body().contains("200")){
                    ToastUtils.show(UpdateUserActivity.this, "头像更新成功");
                    StaticValues.userheads = file;
                    UseGlideUtils.setGlide(UpdateUserActivity.this, file, ivHead);
                    EventBus.getDefault().post(new MessageEvent("更改头像"));
                }else{
                    ToastUtils.show(UpdateUserActivity.this, "头像更新失败");
                }
                loadingDialog.dismiss();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                loadingDialog.dismiss();
            }
        });
    }

    public void ViewInit() {
        tvClassName.setText("个人资料");
        tvClassSubmit.setText("修改");
        tvClassSubmit.setVisibility(View.GONE);
        loadingDialog = new LoadingDialog(this);
        tvUsername.setText(StaticValues.nickname);
        UseGlideUtils.setGlide(UpdateUserActivity.this, StaticValues.userhead, ivHead);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent msg) {
        if (msg.getMessage().equals("更改昵称")) {
            tvUsername.setText(StaticValues.nickname);
        } else if (msg.getMessage().equals("更改头像")) {
            UseGlideUtils.setGlide(this, StaticValues.userheads, ivHead);
        } else if (msg.getMessage().equals("更改密码")) {
            finish();
        }
    }

    @OnClick({R.id.iv_class_back, R.id.tv_class_submit, R.id.rl_head, R.id.rl_username, R.id.rl_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.tv_class_submit:

                break;
            case R.id.rl_head:
                showPicPopWindow(rlHead, "/demain/img/", true);
                break;
            case R.id.rl_username:
                usernameFragment = new UpdateUserNameFragment();
                usernameFragment.show(getSupportFragmentManager(), "tag");
                break;
            case R.id.rl_password:
                passwordFragment = new UpdatePasswordFragment();
                passwordFragment.show(getSupportFragmentManager(), "tag");
                break;
        }
    }
}
